package com.sokal.easycare.modules.position.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.position.entity.Duty;
import com.sokal.easycare.modules.position.entity.PartyRole;
import com.sokal.easycare.modules.position.service.DutyService;
import com.sokal.easycare.modules.position.service.PartyRoleService;

/**
 * 岗位人员Controller
 * @author yangmin
 * @version 2017-04-08
 */
@Controller
@RequestMapping(value = "${adminPath}/position/partyRole")
public class PartyRoleController extends BaseController {

	@Autowired
	private PartyRoleService partyRoleService;
	
	@Autowired
	private DutyService dutyService;
	
	@ModelAttribute
	public PartyRole get(@RequestParam(required=false) String id) {
		PartyRole entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = partyRoleService.get(id);
		}
		if (entity == null){
			entity = new PartyRole();
		}
		return entity;
	}
	
	@RequiresPermissions("position:partyRole:view")
	@RequestMapping(value = {"list", ""})
	public String list(PartyRole partyRole, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PartyRole> page = partyRoleService.findPage(new Page<PartyRole>(request, response), partyRole);
		Duty duty  = new Duty();
		List<Duty> dutyList =dutyService.findList(duty);
		model.addAttribute("dutyList", dutyList);
		model.addAttribute("page", page);
		return "modules/position/partyRoleList";
	}

	@RequiresPermissions("position:partyRole:view")
	@RequestMapping(value = "form")
	public String form(PartyRole partyRole, Model model) {
		Duty duty  = new Duty();
		List<Duty> dutyList =dutyService.findList(duty);
		model.addAttribute("partyRole", partyRole);
		model.addAttribute("dutyList", dutyList);
		return "modules/position/partyRoleForm";
	}

	@RequiresPermissions("position:partyRole:edit")
	@RequestMapping(value = "save")
	public String save(PartyRole partyRole, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partyRole)){
			return form(partyRole, model);
		}
		partyRoleService.save(partyRole);
		addMessage(redirectAttributes, "保存岗位人员成功");
		return "redirect:"+Global.getAdminPath()+"/position/partyRole/?repage";
	}
	
	@RequiresPermissions("position:partyRole:edit")
	@RequestMapping(value = "delete")
	public String delete(PartyRole partyRole, RedirectAttributes redirectAttributes) {
		partyRoleService.delete(partyRole);
		addMessage(redirectAttributes, "删除岗位人员成功");
		return "redirect:"+Global.getAdminPath()+"/position/partyRole/?repage";
	}

}